import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_class2/5778/news/NewsDetailPage.dart';
import 'package:flutter_class2/5778/news/newsCard.dart';
import 'package:url_launcher/url_launcher.dart';

import 'news_json_resp.dart';

class ListBuilder{
  static List<Widget> genWidgesFromJson(NewsData newsData, BuildContext context){
    List<Widget> returnData= new List();
    List<DataItem> dataItems= newsData.result.data.dataItems;
    for(var i=0;i<dataItems.length;i++){
      returnData.add(ListItem.genSingleItem(dataItems[i], context));
    }
    return returnData;
  }
}

class ListItem{
  static Widget genSingleItem(DataItem dataItem, BuildContext context){
    String  uniquekey=dataItem.uniquekey;
    String  title=dataItem.title;
    String  date=dataItem.date;
    String  category=dataItem.category;
    String  author_name=dataItem.author_name;
    String  url=dataItem.url;
    String  thumbnail_pic_s=dataItem.thumbnail_pic_s;
    String  thumbnail_pic_s02=dataItem.thumbnail_pic_s02;
    String  thumbnail_pic_s03=dataItem.thumbnail_pic_s03;

    return Container(
      padding: EdgeInsets.all(10.0),
      child: InkWell(
        onTap: (){
          Navigator.of(context).push(
              MaterialPageRoute(builder: (context)=>NewsDetailPage(title:title, url: url,))
          );
        },
        child: newsCard(
            thumbnail_pic_s,
            title,
            date,
            author_name,
        ),
      ),
    );
  }

  static void launchUrl(String url) async{
    if(await canLaunch(url)){
      await launch(url);
    }else{
      throw 'Could not launch $url';
    }
  }
}